package com.yu.task.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yu.task.mapper.UserBlogMapper;
import com.yu.task.pojo.UserBlog;
import com.yu.task.service.UserBlogService;
import com.yu.task.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserBlogServiceImpl extends ServiceImpl<UserBlogMapper, UserBlog> implements UserBlogService {
    @Autowired
    private UserBlogMapper userBlogMapper;
    @Override
    public int insertUserBlog(UserBlog userBlog) {
        try {
            QueryWrapper<UserBlog> qw = new QueryWrapper<>();
            qw.eq("blog_id",userBlog.getBlogId());
            qw.eq("user_id",userBlog.getUserId());
            UserBlog i = userBlogMapper.selectOne(qw);
            if(i == null){
                userBlog.setCreateTime(new Date());
                int j = userBlogMapper.insert(userBlog);
                return j;
            }else {
                return 0;
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deleteUserBlogById(Long bid,Long uid){
        QueryWrapper<UserBlog> qw = new QueryWrapper<>();
        qw.eq("blog_id",bid);
        qw.eq("user_id",uid);
        int i = userBlogMapper.delete(qw);
        return i;
    }

    @Override
    public Map<String, Object> selectPageUserBlog(int page, int size) {
        HashMap<String,Object> map = new HashMap<>();
        Page<UserBlog> pageObject = new Page<>(page,size);
        try {
            Page page1 = userBlogMapper.selectPage(pageObject,null);
            map.put("code",200);
            map.put("msg","success");
            map.put("data",page1.getRecords());
            map.put("total",page1.getTotal());
        } catch (Exception e) {
            throw new RuntimeException(e);

        }
        return null;
    }
}
